package Actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class reviewerAnaAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){

		forms.adminAnaform ar = (forms.adminAnaform) form;

		String WhichPage = "Rev";		
		ar.setWhichpage(WhichPage);
		
		request.getSession().setAttribute("WhichPage", WhichPage);
		
		String reviewer ="";
		
		String url = "jdbc:postgresql://localhost/Applications";
		Connection conn = null;
		Statement stmt = null;
		Statement stmt2 = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String instruc = null;
		  
		
		try 
		{
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database	
			conn = DriverManager.getConnection(
				"jdbc:postgresql://localhost:5432/Applications", "postgres",
				"postgres");
			// Create the statement
		    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
		                				ResultSet.CONCUR_UPDATABLE);
			
		    stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    				ResultSet.CONCUR_UPDATABLE);
		    
		    rs2 = stmt2.executeQuery("SELECT username, appGraded, appNotGra"+
		    						 " FROM Reviewers order by username");
		
			String maj = null;
			int count = -1;
			
			ar.clearReviewers();
			while( rs2.next() )
			{
				//which Specialization to show on the application page
				String username = rs2.getString("username");
				int appGraded = rs2.getInt("appGraded");
				int appNotGra = rs2.getInt("appNotGra");
				ar.updateReviewers(new forms.reviewerform(username,appGraded,appNotGra));
			
			}
			if(rs != null)
		    	rs.close();
		    rs2.close();
		    stmt.close();
		    stmt2.close();
		    conn.close();		
		} catch (SQLException e) 
		  { 
			System.out.println(e);
		  } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return mapping.findForward("Success");
		
	}
}
